package com.fcscs.ecms.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fcscs.core.dao.ConnectionManager;
import com.fcscs.core.domain.BaseEntity.DBLoc;
import com.fcscs.cservice.domain.MainMenuItem;
import com.fcscs.cservice.ws.WebServiceUtils;
import com.fcscs.cservice.ws.schema.common.HotelRegion;

import com.fcscs.ecms.dao.ECMSHotelRegionDao;

@SuppressWarnings("unchecked")
@Repository("hotelRegionDao")
public class ECMSHotelRegionDaoHibernate implements ECMSHotelRegionDao {

	private static DBLoc TBLoc = new MainMenuItem().getDBLoc();

	@Autowired
	private ConnectionManager connMgr;
	private long orgId;
	
	@Override
	public List<HotelRegion> getHotelRegions(long orgId, long propId, String langId) {
	
		// TODO Auto-generated method stub
		String sql = 
				"SELECT ecms_hotel_staff_v02.mdl_our_hotels_main.regionId," +
				"ecms_hotel_staff_v02.mdl_our_hotels_main.id," + 
				"(select ecms_hotel_staff_v02.global_text.textUnicodeValue from ecms_hotel_staff_v02.global_text " +
					"where languageId = 'en_us' and id = ecms_hotel_staff_v02.global_region.nameRef) as 'Title'	" +	
					"FROM ecms_hotel_staff_v02.mdl_our_hotels_main " +
						 "inner join ecms_hotel_staff_v02.global_region on global_region.id = ecms_hotel_staff_v02.mdl_our_hotels_main.regionId " +
						 "inner join ecms_hotel_staff_v02.global_hotel_entity on ecms_hotel_staff_v02.global_region.id = ecms_hotel_staff_v02.global_hotel_entity.regionId " +
						 "where ecms_hotel_staff_v02.mdl_our_hotels_main.proprietaryId = 1 and ecms_hotel_staff_v02.mdl_our_hotels_main.isActive = true " +
						"order by  ecms_hotel_staff_v02.mdl_our_hotels_main.displayOrder";
                        
		Query sqlQu = connMgr.getCurrentSession(TBLoc).createSQLQuery(sql);

		List<HotelRegion> listHotelRegions = new ArrayList<HotelRegion>();
		List<Object[]> objects = (List<Object[]>)sqlQu.list();
		for(Object[] objTmp : objects){
			HotelRegion hotelRegion = new HotelRegion();
			for(int i =0 ; i<objTmp.length ; i++){
				System.out.println(String.valueOf(objTmp[i]));
				
			}
			hotelRegion.setId(Long.parseLong(String.valueOf(objTmp[1])));
			hotelRegion.setTitle(String.valueOf(objTmp[2]));
			listHotelRegions.add(hotelRegion);			
		}
		
		return listHotelRegions;
	}

}
